package experiment.e08.code;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Objects;

/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/11/17 13:14
 * @package experiment.e08.code
 * @description 编写一个应用程序，设计一个窗体，其中有一个JCombox组件（+、-、*、/），
 * 以及2个文本框和一个富文本框，单击相应的按钮，将两个文本框的数字做算术运算，
 * 在第3个文本框中显示结果。要求处理NumberFormatException。
 */

public class Ch01 {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("简单的计算器");
        jFrame.setBounds(500, 400, 400, 400);
        jFrame.setLayout(new BorderLayout());

        // 定义计算面板
        JPanel comparePanel = new JPanel();
        // 定义组件
        JTextField num1 = new JTextField(5);
        JComboBox<String> symbolComboBox = new JComboBox<>(new String[]{"+", "-", "*", "/"});
        symbolComboBox.setBounds(52, 10, 300, 300);
        JTextField num2 = new JTextField(5);
        JLabel jLabel = new JLabel("=");
        JButton jButton = new JButton("计算");

        // 面板添加组件
        comparePanel.add(num1);
        comparePanel.add(symbolComboBox);
        comparePanel.add(num2);
        comparePanel.add(jLabel);
        comparePanel.add(jButton);

        Dialog errorDialog = new Dialog(jFrame, "提示框", true);
        errorDialog.setBounds(590, 480, 150, 150);
        errorDialog.add(new JLabel("输入错误，请输入正确的数字后重试"));
        errorDialog.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        // 定义显示面板
        JPanel showPanel = new JPanel();
        // 定义组件
        TextArea showTextArea = new TextArea(10, 40);
        showPanel.add(showTextArea);

        // 定义按钮的监听事件
        jButton.addActionListener(e -> {
            try {
                // 先取出数字
                double num_1 = Double.parseDouble(num1.getText());
                double num_2 = Double.parseDouble(num2.getText());
                // 取出当前选中的符号
                String selectedItem = (String) symbolComboBox.getSelectedItem();
                // 计算结果
                double result_num = 0;
                switch (Objects.requireNonNull(selectedItem)) {
                    case "+":
                        result_num = num_1 + num_2;
                        break;
                    case "-":
                        result_num = num_1 - num_2;
                        break;
                    case "*":
                        result_num = num_1 * num_2;
                        break;
                    case "/":
                        try {
                            result_num = num_1 / num_2;
                        } catch (Exception exception) {
                            exception.printStackTrace();
                        }
                        break;
                }
                // 将结果添加到富文本框中
                showTextArea.append(num_1 + selectedItem + num_2 + "=" + result_num + "\n");
                num1.setText("");
                num2.setText("");
            } catch (NumberFormatException numberFormatException) {
                numberFormatException.printStackTrace();
                errorDialog.setVisible(true);
            }
        });

        // 将面板添加到frame 中
        jFrame.add(comparePanel, BorderLayout.NORTH);
        jFrame.add(showPanel, BorderLayout.CENTER);

        jFrame.setVisible(true);

    }
}